*******************************
* SUMMARY STATS: CLARITY DATA *
*******************************
* Recall: sample 2 is the linked data; sample 1 is the random sample
********************
* Variable Recodes *
********************
foreach num of numlist 1 2 {
	******** Inquiries *******
	use "$build/payday_inquiry_level_fin`num'.dta", clear

	gen online=(inquiry_tradeline_type=="C3" | inquiry_tradeline_type=="C1")
	gen storefront=(inquiry_tradeline_type=="C2" | inquiry_tradeline_type=="C6")
	gen other=(online==0 & storefront==0)

	save "$build/payday_inq_fin`num'_clean.dta", replace
	
	***** Tradelines ********
	use "$build/payday_trade_level_fin`num'.dta", clear

	destring terms_frequency, force gen(days)
	destring terms_duration, force gen(dur_months)
	replace dur_months=. if terms_duration=="1" | terms_duration=="001"

	gen single_lt_month=(terms_duration=="1" & (terms_frequency=="W" | terms_frequency=="7" | terms_frequency=="B" | | terms_frequency=="14" ///
							| terms_frequency=="E" | terms_frequency=="15" | terms_frequency=="M" | terms_frequency=="30" | days<=31))
	gen single_gt_month=(terms_duration=="1" & (terms_frequency=="L" | terms_frequency=="60" | terms_frequency=="Q" | terms_frequency=="90"    ///
						 | terms_frequency=="T" | terms_frequency=="120"  | terms_frequency=="S" | terms_frequency=="180"  | terms_frequency=="Y" | terms_frequency=="365" 	| (days>31 & days<.))) 
	gen installment_lt_month=(dur_months>1 & dur_months<. & (terms_frequency=="W" | terms_frequency=="7" | terms_frequency=="B" | | terms_frequency=="14" ///
							| terms_frequency=="E" | terms_frequency=="15" | terms_frequency=="M" | terms_frequency=="30" | days<=31))
	gen installment_gt_month=(dur_months>1 & dur_months<. & (terms_frequency=="L" | terms_frequency=="60" | terms_frequency=="Q" | terms_frequency=="90"    ///
						 | terms_frequency=="T" | terms_frequency=="120"  | terms_frequency=="S" | terms_frequency=="180"  | terms_frequency=="Y" | terms_frequency=="365" 	| (days>31 & days<.))) 

	gen other_duration=(single_lt_month==0 & single_gt_month==0 & installment_lt_month==0 & installment_gt_month==0)

	save "$build/payday_trades_fin`num'_clean.dta", replace
}

**********************
* Summary statistics *
**********************
foreach samp of numlist 1 2 {
	local vnum=1
	foreach var in "online" "storefront" "other" "highest_credit" "single_lt_month" "single_gt_month" "installment_lt_month" "installment_gt_month" "other_duration" {

		if "`var'"=="online" | "`var'"=="storefront" | "`var'"=="other"  {
			use "$build/payday_inq_fin`samp'_clean.dta", clear
			}
		if "`var'"~="online" & "`var'"~="storefront" & "`var'"~="other"  {
			use "$build/payday_trades_fin`samp'_clean.dta", clear
			}	

		sum `var'
		local b`vnum'_`samp': di  %9.2fc r(mean)  
		local sd`vnum'_`samp': di %4.2fc r(sd) 
		
		local vnum=`vnum'+1
	}
}

*Get observation counts
foreach samp of numlist 1 2 {
	foreach name in "inq" "trades" {
			use "$build/payday_`name'_fin`samp'_clean.dta", clear
			count 
			local countN_`name'_`samp': di %12.2gc r(N)
			
			bys RECORD_NB: gen first=_n==1
			count if first==1
			local countindiv_`name'_`samp': di %12.2gc r(N)
		}
}
	
*******************
* EXPORT TO LATEX *
*******************
texdoc init "${Inputs}\Cook_payday_sumstats.tex", replace force	
tex { \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{lc*{3}{c}} \toprule ///
                &\multicolumn{1}{c}{Random Sample}     &\multicolumn{1}{c}{Eviction court sample}    \\
tex				\cmidrule(lr){2-2}  \cmidrule(lr){3-3}      &\multicolumn{1}{c}{(1)}         &\multicolumn{1}{c}{(2)}            \\
tex \midrule \vspace{.2cm}\textbf{I.} \emph{Inquiries} &   &    	\\
tex  Online  								  &  `b1_1' 	&   `b1_2'		\\
tex           								  & (`sd1_1')   &  (`sd1_2')  	 \\
tex  Storefront  							  &  `b2_1' 	&   `b2_2'		\\
tex           								  & (`sd2_1')   &  (`sd2_2')  	 \\
tex  Other Type of Inquiry					  &  `b3_1' 	&   `b3_2'		\\
tex           								  & (`sd3_1')   &  (`sd3_2')  	 \\
tex  Number of Inquiries					  &  `countN_inq_1' 	&   `countN_inq_2'		\\
tex  Number of Unique Individuals			  &  `countindiv_inq_1' 	&   `countindiv_inq_2'		\\
tex \\
tex \midrule \vspace{.2cm}\textbf{II.} \emph{Accounts} &   &   	\\
tex  Amount  								 &  `b4_1' 	&   `b4_2'		 \\
tex           						 		 & (`sd4_1')   &  (`sd4_2')     \\ 
tex  Single Payment, Duration: $\le$ 1 Month &  `b5_1' 	&   `b5_2'		 \\
tex           					 			 & (`sd5_1')   &  (`sd5_2')     \\ 
tex  Single Payment, Duration: $>$ 1 Month  &  `b6_1' 	&   `b6_2'		 \\
tex           		 			 			& (`sd6_1')   &  (`sd6_2')     \\ 
tex  Installment, Duration: $\le$ 1 Month   &  `b7_1' 	&   `b7_2'		 \\
tex           					  			& (`sd7_1')   &  (`sd7_2')     \\ 
tex  Installment, Duration: $>$ 1 Month  	&  `b8_1' 	&   `b8_2'		 \\
tex           		 			 			& (`sd8_1')   &  (`sd8_2')     \\ 
tex  Other Type of Account					&  `b9_1' 	&   `b9_2'		 \\
tex           					  			& (`sd9_1')   &  (`sd9_2')     \\ 
tex  Number of Accounts					  &  `countN_trades_1' 	&   `countN_trades_2'		\\
tex  Number of Unique Individuals			  &  `countindiv_trades_1' 	&   `countindiv_trades_2'		\\
tex \bottomrule \end{tabular} }
texdoc close
